﻿using System.Collections.Generic;
using System.Data;
using Pku.BookStore.Model;
using System;

namespace Pku.BookStore.DAL
{
    /// <summary>
    /// 数据访问类
    /// </summary>
    public class OrderDetailDAL : BaseDAL
    {
        string commandText = string.Empty;

        /// <summary>
        /// 增加订单详情信息
        /// </summary>
        /// <param name="entity"></param>
        public int Add(OrderDetailModel entity)
        {
            commandText = @"INSERT INTO OrderDetail(OrderID,BookID,UnitPrice,Quantity,PublisherID)
VALUES(@OrderID,@BookID,@UnitPrice,@Quantity,@PublisherID);
";
            var command = DbMain.GetSqlStringCommand(commandText);
            DbMain.AddInParameter(command, "OrderID", DbType.Int32, entity.OrderID);
            DbMain.AddInParameter(command, "BookID", DbType.Int32, entity.BookID);
            DbMain.AddInParameter(command, "UnitPrice", DbType.Decimal, entity.UnitPrice);
            DbMain.AddInParameter(command, "Quantity", DbType.Int32, entity.Quantity);
            DbMain.AddInParameter(command, "PublisherID", DbType.Int32, entity.PublisherID);

            return Convert.ToInt32(DbMain.ExecuteNonQuery(command) == 1);

        }

        /// <summary>
        /// 修改订单详情信息
        /// </summary>
        /// <param name="entity"></param>
        public bool Modify(OrderDetailModel entity)
        {
            commandText = @"UPDATE OrderDetail SET OrderID=@OrderID,BookID=@BookID,UnitPrice=@UnitPrice,Quantity=@Quantity,PublisherID=@PublisherID WHERE OrderID=@OrderID";
            var command = DbMain.GetSqlStringCommand(commandText);
            DbMain.AddInParameter(command, "OrderID", DbType.Int32, entity.OrderID);
            DbMain.AddInParameter(command, "BookID", DbType.Int32, entity.BookID);
            DbMain.AddInParameter(command, "UnitPrice", DbType.Decimal, entity.UnitPrice);
            DbMain.AddInParameter(command, "Quantity", DbType.Int32, entity.Quantity);
            DbMain.AddInParameter(command, "PublisherID", DbType.Int32, entity.PublisherID);

            return (int)DbMain.ExecuteNonQuery(command) == 1;
        }

        /// <summary>
        /// 搜索订单详情
        /// </summary>
        /// <returns></returns>
        public List<OrderDetailModel> Search(OrderDetailParam param)
        {
            List<OrderDetailModel> list = new List<OrderDetailModel>();
            commandText = @"SELECT OrderID,BookID,UnitPrice,Quantity,PublisherID  FROM  OrderDetail";
            using (var dr = DbMain.ExecuteReader(CommandType.Text, commandText))
            {
                while (dr.Read())
                {
                    list.Add(ConvertReaderToEntity(dr));
                }
            }
            return list;
        }

        /// <summary>
        /// 获取订单详情实体
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public List<OrderDetailModel> GetListById(int orderID)
        {
            List<OrderDetailModel> list = new List<OrderDetailModel>();
            commandText = @"SELECT 
	                            od.OrderDetailID,
	                            od.OrderID,
	                            od.BookID,
	                            od.UnitPrice,
	                            od.Quantity,
	                            od.PublisherID,
	                            p.BookName,
	                            p.[Image]
                            FROM 
	                            OrderDetail od(NOLOCK)
	                            INNER JOIN Product p(NOLOCK) ON od.BookID=p.BookID
                            WHERE
	                            od.OrderID=@orderID";
            var command = DbMain.GetSqlStringCommand(commandText);
            DbMain.AddInParameter(command, "OrderID", DbType.Int32, orderID);
            using (var dr = DbMain.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    var m = ConvertReaderToEntity(dr);
                    m.BookName = dr["BookName"].ToString();
                    m.Image = dr["Image"].ToString();
                    list.Add(m);
                }
            }
            return list;
        }

        /// <summary>
        /// 获取订单详情实体
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public OrderDetailModel GetEntityById(int orderID)
        {
            commandText = @"SELECT OrderID,BookID,UnitPrice,Quantity,PublisherID  FROM  OrderDetail WHERE OrderID=@OrderID";
            var command = DbMain.GetSqlStringCommand(commandText);
            DbMain.AddInParameter(command, "OrderID", DbType.Int32, orderID);
            using (var dr = DbMain.ExecuteReader(command))
            {
                if (dr.Read())
                {
                    return ConvertReaderToEntity(dr);
                }
            }
            return null;
        }

        /// <summary>
        /// 转换实体
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        private static OrderDetailModel ConvertReaderToEntity(IDataRecord dr)
        {
            return new OrderDetailModel
            {
                OrderID = (int)dr["OrderID"],
                BookID = (int)dr["BookID"],
                UnitPrice = (decimal)dr["UnitPrice"],
                Quantity = (int)dr["Quantity"],
                PublisherID = (int)dr["PublisherID"]
            };
        }
    }

}